package com.ossjk.oa.accounting.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonMethod;
import com.ossjk.oa.accounting.entity.CwCapitals;
import com.ossjk.oa.accounting.service.ICwCapitalsService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chair
 * @since 2020-05-14
 */
@RestController
@RequestMapping("/accounting/cwCapitals")
//@CrossOrigin
public class CwCapitalsController extends BaseController {

	@Autowired
	private ICwCapitalsService iCwCapitalsService;

	@RequiresPermissions("0000")
	@RequestMapping("/list")
	public Object list(String mth, HttpServletRequest req) {

		if (mth == null) {
			// 先去session中看有无旧值
			if (req.getSession().getAttribute("mth4Cps") != null) {
				mth = (String) req.getSession().getAttribute("mth4Cps");
			} else {
				mth = CommonMethod.calMth(0, new Date());
				if (iCwCapitalsService.countByMth(mth) == 0) {
					mth = CommonMethod.calMth(-1, new Date());
				}
				req.getSession().setAttribute("mth4Cps", mth);
			}
		} else {
			req.getSession().setAttribute("mth4Cps", mth);
		}
		// System.out.println("mth = "+mth);
		Map datas = new HashMap();
		datas.put("navi", iCwCapitalsService.selectPreOrNextByMth(mth));
		datas.put("datas", iCwCapitalsService.selectCapitalsByMth(mth));

		return responseSuccess(datas);

	}

	@PostMapping("/insert")
	public Object insert(CwCapitals cw_capitals) {
		 
		cw_capitals.setFlag(0);
		if (iCwCapitalsService.insert(cw_capitals)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@GetMapping("/cfm/{id}")
	public Object cfm(@PathVariable("id") String id,String needrecDate) {
		if(iCwCapitalsService.cwUpdateCapitals(id,needrecDate)){
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@DeleteMapping("/delete/{id}")
	public Object deleteOne(@PathVariable("id") String id) {

		// Wrapper wrapper = Condition.create().where("id", id);
		if (iCwCapitalsService.deleteById(id)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

}
